<?php

/*
	Class: WatchFeedItems

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>

	About: See Also
	 	<Cts_Db_Table_Abstract>
*/
class WatchFeedItems extends Cts_Db_Table_Abstract {

	/* Group: Instance Variables */

	/*
		Variable: $_name
			The name of the table or view to interact with in the data source.
	*/
	protected $_name = 'watch_feeditems';

	/*
		Variable: $_primary
			The primary key of the table or view to interact with in the data source.
	*/
	protected $_primary = 'id';

	/* Group: Instance Methods */

	/*
		Function: getItemById
			Gets a feed item from the database by ID.

		Arguments:
			feed_item_id - The ID of the feed item to get.

		Returns: a feed item as an array, or null
	*/
	function getItemById($feed_item_id) {
		$where = $this->getAdapter()->quoteInto("id = ?", $feed_item_id);
		$tmp_feed_item = $this->fetchRow($where);
		if (!is_null($tmp_feed_item)) {
			return $tmp_feed_item->toArray();
		} else {
			return null;
		}
	}

	/*
		Function: insertItem
			Inserts a feed item for a subscription.
			This is typically called by a plugin when content in the site has changed.

		Arguments:
			username - The username of the subscription. Part of a composite foreign key.
			tablename - The tablename of the subscription. Part of a composite foreign key.
			itemid - The itemid of the subscription. Part of a composite foreign key.
			author - The author of the content being represented by the feed item.
			title - The title of the feed item.
			url - The URL to the content being represented by the feed item.
			description - A description of the feed item and/or the content it represents.
			status - Possible values are 'unread', 'read', 'starred', or 'archived'
			thumb_url - An optional URL to a thumbnail image.

		Returns: the new feed item ID (int)
	*/
	function insertItem($username, $tablename, $itemid, $author, $title, $url, $description, $status = null, $thumb_url = null) {
		$data = array(
			'username' => $username,
			'tablename' => $tablename,
			'itemid' => $itemid,
			'thumb_url' => $thumb_url,
			'author' => $author,
			'title' => $title,
			'url' => $url,
			'description' => $description,
			'status' => $status,
			'created_on' => date("Y-m-d H:i:s"),
			'last_modified_on' => date("Y-m-d H:i:s")
		);
		return $this->insert($data);
	}

	/*
		Function: deleteItem

		Arguments:
			id - The ID of the feed item to delete. Not to be confused with the itemid field which is part of a composite foreign key.
			username - The username is optional (recommended) if you want to validate that the item belongs to the user.

		Returns: void
	*/
	function deleteItem($id, $username = null) {
		$where = $this->getAdapter()->quoteInto('id = ?', $id);
		if (!is_null($username)) {
			$where .= $this->getAdapter()->quoteInto(' and username = ?', $username);
		}
		$this->delete($where);
	}

	/*
		Function: getItems

		Arguments:
			$username - The username of the subscriber. Feed items will only be retrieved where the username column matches this argument's value.
			$order_by - An order by clause in the chosen model query language, such as SQL. Defaults to order by most recent changes first ("last_modified_on desc").

		Returns: array of feed items or an empty array
	*/
	function getItems($username, $order_by = "last_modified_on desc") {
		$where = $this->getAdapter()->quoteInto("username = ?", $username);
		$tmp_items = $this->fetchAll($where, $order_by);
		if (!is_null($tmp_items)) {
			$tmp_items = $tmp_items->toArray();
			$tmp_items = $this->addHowLongAgo($tmp_items);
			return $tmp_items;
		} else {
			return array();
		}
	}

	/*
		Function: getItemsCountByUsername

		Arguments:
			$username - The username of the subscriber. Feed items will only be retrieved where the username column matches this argument's value.

		Returns: integer (zero or greater)
	*/
	function getItemsCount($username) {
		$tmp_items = $this->getItems($username);
		return count($tmp_items);
	}

	/*
		Function: getItemsPage

		Arguments:
			$username - The username of the subscriber. Feed items will only be retrieved where the username column matches this argument's value.
			$per_page - The maximum number of feed items to return.
			$page - The index of the page you want to get. Zero-based, and defaults to zero (the first page).
			$order_by - An order by clause in the chosen model query language, such as SQL. Defaults to order by most recent changes first ("last_modified_on desc").

		Returns: array of feed items or an empty array
	*/
	function getItemsPage($username, $per_page, $page = 0, $order_by = "last_modified_on desc") {
		$where = $this->getAdapter()->quoteInto("username = ?", $username);
		$tmp_items = $this->fetchAll($where, $order_by, $per_page, $per_page * $page);
		if (!is_null($tmp_items)) {
			$tmp_items = $tmp_items->toArray();
			$tmp_items = $this->addHowLongAgo($tmp_items);
			return $tmp_items;
		} else {
			return array();
		}
	}

	/*
		Function: getItemsInDateRange

		Arguments:
			$username - The username of the subscriber. Feed items will only be retrieved where the username column matches this argument's value.
			$start - The lower bounds of the date range. Items created before this date/time will not be included.
			$end - The upper bounds of the date range. Items created after this date/time will not be included.
			$order_by - An order by clause in the chosen model query language, such as SQL. Defaults to order by most recent changes first ("last_modified_on desc").

		Returns: array of feed items or an empty array
	*/
	function getItemsInDateRange($username, $start, $end, $order_by = "last_modified_on desc") {
		$where = $this->getAdapter()->quoteInto("username = ?", $username);
		$where .= $this->getAdapter()->quoteInto(" and created_on between ? and ?", array($start, $end));
		$tmp_items = $this->fetchAll($where, $order_by);
		if (!is_null($tmp_items)) {
			$tmp_items = $tmp_items->toArray();
			$tmp_items = $this->addHowLongAgo($tmp_items);
			return $tmp_items;
		} else {
			return array();
		}
	}

	/* Group: Private or Protected Methods */

	/*
		Function: addHowLongAgo
			Adds a key-value pair to each item in an array of feed items which
			contains a phrase describing how long ago the item was created.
			For now it's just United States English (en-US) but localization will eventually be supported.

		Arguments:
			$feeditems - An array of feed items, such as those generated by <getItemsCountByUsername> or <getItemsPage>.

		Returns: array of feed items, same as passed in but with an additional key-value pair added
	*/
	protected function addHowLongAgo($feeditems) {
		$new_feeditems = array();
		foreach ($feeditems as $feeditem) {
			$feeditem['howlongago'] = Cts_Common::xyago(strtotime($feeditem['created_on']));
			$new_feeditems[] = $feeditem;
		}
		return $new_feeditems;
	}

}
